package com.hoshiicloud.market.service.impl.commission;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.market.entity.commission.CommissionRecords;
import com.hoshiicloud.market.feign.IdGeneratorService;
import com.hoshiicloud.market.mapper.commission.CommissionRecoedsMapper;
import com.hoshiicloud.market.service.commission.CommissionRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @Author:campchen
 * @Date:Created in 下午 01:54 2019/7/27
 * @PACKAGENAME com.hoshiicloud.market.service.impl.commission
 * @Description
 */
@Service
public class CommissionRecordsServiceImpl extends ServiceImpl<CommissionRecoedsMapper, CommissionRecords> implements CommissionRecordsService {
    @Autowired
    private IdGeneratorService idGeneratorService;

    @Cacheable(cacheNames = "CommissionRecords", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Override
    public CommissionRecords getCommissionRecordsById(Long id) {
        return baseMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "CommissionRecords", allEntries = true)
    @Override
    public String insert(CommissionRecords commissionRecords) {
        String id = idGeneratorService.queryGeneratorId();
        commissionRecords.setCreatedtime(LocalDateTime.now());
        commissionRecords.setDeleted(0);
        commissionRecords.setTxdate(LocalDateTime.now());
        commissionRecords.setId(Long.parseLong(id));
        int row = baseMapper.insert(commissionRecords);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return id;
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "CommissionRecords", allEntries = true)
    @Override
    public boolean updateById(CommissionRecords commissionRecords) {
        commissionRecords.setUpdatedtime(LocalDateTime.now());
        int row = baseMapper.updateById(commissionRecords);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    @Cacheable(cacheNames = "CommissionRecords", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Override
    public IPage<CommissionRecords> getPage(IPage<CommissionRecords> page, Wrapper<CommissionRecords> queryWrapper, String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }
}
